{% extends 'dns_manager/base.html' %}
{% load crispy_forms_tags %}

{% block title %}添加云账号 - DNS管理系统{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0">
                    <i class="bi bi-cloud-plus me-2"></i>添加云账号
                </h4>
            </div>
            
            <div class="card-body p-4">
                <form method="post">
                    {% csrf_token %}
                    
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">账号名称 *</label>
                            {{ form.name }}
                            {% if form.name.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.name.errors.0 }}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">云服务商 *</label>
                            {{ form.provider }}
                            {% if form.provider.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.provider.errors.0 }}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="col-12">
                            <label class="form-label fw-semibold">Access Key *</label>
                            {{ form.access_key }}
                            {% if form.access_key.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.access_key.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                从云服务商控制台获取的访问密钥ID
                            </small>
                        </div>
                        
                        <div class="col-12">
                            <label class="form-label fw-semibold">Secret Key *</label>
                            {{ form.secret_key }}
                            {% if form.secret_key.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.secret_key.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                访问密钥的私钥，将被加密存储
                            </small>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">地域</label>
                            {{ form.region }}
                            {% if form.region.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.region.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                可选，部分服务商需要指定地域
                            </small>
                        </div>
                        
                        <div class="col-md-6 d-flex align-items-end">
                            <div class="form-check">
                                {{ form.is_active }}
                                <label class="form-check-label fw-semibold" for="{{ form.is_active.id_for_label }}">
                                    启用此账号
                                </label>
                            </div>
                        </div>
                    </div>
                    
                    <hr class="my-4">
                    
                    <!-- 云服务商配置说明 -->
                    <div class="alert alert-info">
                        <h6 class="alert-heading">
                            <i class="bi bi-info-circle me-2"></i>配置说明
                        </h6>
                        <div class="row g-3 small">
                            <div class="col-md-6">
                                <strong>阿里云:</strong>
                                <ul class="mb-0 mt-1">
                                    <li>地域建议: cn-hangzhou</li>
                                    <li>需要RAM权限: AliyunDNSFullAccess</li>
                                </ul>
                            </div>
                            <div class="col-md-6">
                                <strong>腾讯云:</strong>
                                <ul class="mb-0 mt-1">
                                    <li>地域建议: ap-beijing</li>
                                    <li>需要CAM权限: QcloudDNSPodFullAccess</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check me-1"></i>保存账号
                        </button>
                        <a href="{% url 'cloud_accounts' %}" class="btn btn-outline-secondary">
                            <i class="bi bi-x me-1"></i>取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 根据选择的云服务商显示不同的地域建议
    const providerSelect = document.querySelector('select[name="provider"]');
    const regionInput = document.querySelector('input[name="region"]');
    
    if (providerSelect && regionInput) {
        providerSelect.addEventListener('change', function() {
            const provider = this.value;
            let suggestedRegion = '';
            
            switch(provider) {
                case 'aliyun':
                    suggestedRegion = 'cn-hangzhou';
                    break;
                case 'tencent':
                    suggestedRegion = 'ap-beijing';
                    break;
                case 'huawei':
                    suggestedRegion = 'cn-north-1';
                    break;
                case 'volcengine':
                    suggestedRegion = 'cn-beijing';
                    break;
            }
            
            if (suggestedRegion && !regionInput.value) {
                regionInput.value = suggestedRegion;
            }
        });
    }
});
</script>
{% endblock %}